package org.studle.services;

import org.studle.domain.*;
import org.studle.domain.exceptions.NonExistentElementException;
import org.studle.data.*;

//XXX remember - exceptions is a part of contract. Think about alternate flows of your app.
public class AttendeeService {
	private GenericDAO<String, Attendee> attendeeDataAccesser; //= new GenericDAO<String, Attendee>();
	
	public AttendeeService(GenericDAO<String, Attendee> attendeeDataAccesser) {
		this.attendeeDataAccesser = attendeeDataAccesser;
	}

	public void createPersonAccount(String name, String email, String password) throws IllegalArgumentException{
			attendeeDataAccesser.createRecord(email, new Person(name, email, password));
	}
	
	//TODO what about potential errors?
	public void updatePersonAccount(String Email, String name, String password) throws NonExistentElementException{
		
		//[ek] TODO you should make this code works without casting to Person
		//Person person = attendeeDataAccesser.getByKey(Email);
		//Why had you pain with generics if your design is not supports it? 
		Person person = (Person) attendeeDataAccesser.getRecordByPrimaryKey(Email);
		person.setName(name);
		person.setPassword(password);
	}

	public void deletePersonAccount(Person person) throws NonExistentElementException{
		attendeeDataAccesser.deleteRecord(person.getEmail());
	}
}
